package de.g18.it0a.jlaser.util;

import java.util.Arrays;


public class StringUtil {

    private StringUtil() {
        // prevent instantiation
    }

    /**
     * @return the offset of the first not matching char or -1 if everything is fine.
     */
    public static int isValidNumber(String aNumber) {
        if (aNumber == null || aNumber.isEmpty()) {
            return 0;
        }
        return containsOnly(aNumber, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    }

    /**
     * @return the offset of the first not matching char or -1 if everything is fine.
     */
    public static int containsOnly(String aString, String aCharsToContainOnly) {
        return containsOnly(aString, aCharsToContainOnly.toCharArray());
    }

    /**
     * @return the offset of the first not matching char or -1 if everything is fine.
     */
    public static int containsOnly(String aString, char... aCharsToContainOnly) {
        Arrays.sort(aCharsToContainOnly);
        int offset = -1;
        for (char c : aString.toCharArray()) {
            offset++;
            if (Arrays.binarySearch(aCharsToContainOnly, c) < 0) {
                return offset;
            }
        }
        return -1;
    }

}
